﻿using System;
using System.Data;
using System.IO.Ports;
using System.Linq;
using System.Windows.Forms;
using 右下角弹窗.DiagShow;

namespace LD.Host
{
    public partial class FrmHD : BaseFrm
    {
        public FrmHD()
        {
            InitializeComponent();
            this.Shown += FrmVariable_Shown;
        }

        private void FrmVariable_Shown(object sender, EventArgs e)
        {
            base.Permission(UserType.管理员);

            if (this.IsDisposed)
                return;


            Loader();
        }
        private void 删除选中行ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            var row = this.dgv_data.CurrentRow;
            if (row != null)
            {
                string name = row.Cells["名称"].Value.ToString();
                var remove = CommunicateFactory.Instance.GetList.Where(p => p.Name == name).FirstOrDefault();
                if (remove != null)
                {
                    CommunicateFactory.Instance.GetList.Remove(remove);
                    CommunicateFactory.Instance.Save();
                }

                Loader();

                Dialog dialog = new Dialog();
                dialog.Warning("重启生效");
            }
        }
        private void UI()
        {
            DataTable xieyi = Get协议();
            DataTable jiaoyan = Get效验();
            DataTable plc = GetPLC();
        }
        private void Loader()
        {
            this.dgv_data.Rows.Clear();
            if (CommunicateFactory.Instance.GetList == null)
                return;
            foreach (var item in CommunicateFactory.Instance.GetList)
            {
                int index = this.dgv_data.Rows.Add();
                this.dgv_data["名称", index].Value = item.Name;
                this.dgv_data["地址", index].Value = item.Address;
                this.dgv_data["端口", index].Value = item.Port;
                this.dgv_data["超时时间", index].Value = item.TimeOut;
                this.dgv_data["协议", index].Value = item.HDType > -1 ? Enum.GetName(typeof(HDType), item.HDType) : "";
                this.dgv_data["效验", index].Value = item.Parity > -1 ? Enum.GetName(typeof(Parity), item.Parity) : "";
                this.dgv_data["PLC", index].Value = item.Model > -1 ? Enum.GetName(typeof(PLCType), item.Model) : "";
                this.dgv_data["心跳", index].Value = item.Heartbeat;
            }
        }
        public void New()
        {
            FrmHDEdit edit = new FrmHDEdit();
            if (edit.ShowDialog() == DialogResult.OK)
                Loader();
        }
        public void MRefresh()
        {
            Loader();
        }
        public void Save()
        {
            CommunicateFactory.Instance.Save();
            Dialog dialog = new Dialog();
            dialog.Warning("重启生效");
        }
        private DataTable Get协议()
        {
            string[] str = Enum.GetNames(typeof(HDType)).ToArray();
            DataTable dt = new DataTable();
            dt.Columns.Add("id");
            dt.Columns.Add("name");
            for (int i = 0; i < str.Length; i++)
            {
                dt.Rows.Add(i, str[i]);
            }
            return dt;
        }
        private DataTable Get效验()
        {
            string[] str = Enum.GetNames(typeof(Parity)).ToArray();
            DataTable dt = new DataTable();
            dt.Columns.Add("id");
            dt.Columns.Add("name");
            for (int i = 0; i < str.Length; i++)
            {
                dt.Rows.Add(i, str[i]);
            }
            return dt;
        }
        private DataTable GetPLC()
        {
            string[] str = Enum.GetNames(typeof(PLCType)).ToArray();
            DataTable dt = new DataTable();
            dt.Columns.Add("id");
            dt.Columns.Add("name");
            for (int i = 0; i < str.Length; i++)
            {
                dt.Rows.Add(i, str[i]);
            }
            return dt;
        }
        private void dgv_data_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
        {
            if (e.ColumnIndex > -1 && e.RowIndex > -1)
            {
                var row = this.dgv_data.CurrentRow;

                if (row != null)
                {
                    string name = row.Cells["名称"].Value.ToString();
                    FrmHDEdit edit = new FrmHDEdit(name);
                    if (edit.ShowDialog() == DialogResult.OK)
                    {
                        Loader();
                    }
                }
            }
        }
    }
}
